package com.school.service.impl;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.school.dao.HljtTeacherCurriculumoDao;
import com.school.entity.HljtClass;
import com.school.entity.HljtTeacher;
import com.school.entity.web.TeacherCurriculumInfo;
import com.school.service.HljtTeacherCurriculumService;
import com.school.util.MapperToJson;
import com.school.util.RepMessage;
import com.school.util.WhetherNotToUpdate;

@Service
public class HljtTeacherCurriculumServiceImpl implements HljtTeacherCurriculumService{

	@Autowired
	HljtTeacherCurriculumoDao oHljtTeacherCurriculumoDao;
	
	@Override
	public String findAllTeacherAndClasses(int nType,String strPath,String name,int pageIndex,int pageSize) {
		pageIndex=(pageIndex-1)*pageSize;
		List<HljtTeacher> tList=oHljtTeacherCurriculumoDao.findAllTacherInfo(nType); 
		List<HljtClass> cList=oHljtTeacherCurriculumoDao.findAllClasses();
		List<TeacherCurriculumInfo> mList=this.findCurriculumInfo(nType,name,pageIndex,pageSize);
		int nums=pageIndex+pageSize ;
		if(mList.size()>=nums){
			mList=mList.subList(pageIndex,pageIndex+pageSize );
		}
		int Sum=this.findTeacherCurriculumInfoCount(nType, name);
		for (TeacherCurriculumInfo teacherCurriculumInfo : mList) {
			try {
				teacherCurriculumInfo.setImages(WhetherNotToUpdate.getIp()+teacherCurriculumInfo.getImages());
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
		}
		for (HljtTeacher tea : tList) {
			try {
				tea.setImages(WhetherNotToUpdate.getIp()+tea.getImages());
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
		}
		HashMap<String,Object> oHashMap=new HashMap<String,Object>();
		oHashMap.put("allTeacher", tList);
		oHashMap.put("allClasses", cList);
		oHashMap.put("allCurriculumInfo", mList);
		oHashMap.put("sum", Sum);
		return MapperToJson.toJson(oHashMap);
	}
	@Override
	public int findTeacherCurriculumInfoCount(int nType,String name){
		int nCount=0;
		if(name==""||name ==null){
			if(nType==1){
			nCount=	oHljtTeacherCurriculumoDao.findHCurriculumInfoCount(nType);
			}else{
			nCount=	oHljtTeacherCurriculumoDao.findCurriculumInfoCount();
			}
		}else{
			if(nType==1){
			nCount=	oHljtTeacherCurriculumoDao.findHCurriculumInfoCountByName(nType, name);
			}else{
			nCount=	oHljtTeacherCurriculumoDao.findCurriculumInfoCountByName(name);
			}
		}
		return nCount;
	}
	
	@Override
	public List<TeacherCurriculumInfo> findCurriculumInfo(int nType,String name,int pageIndex,int pageSize) {
		List<TeacherCurriculumInfo> list=new ArrayList<TeacherCurriculumInfo>();
		if(name==""||name ==null){
			if(nType==1){
				list=oHljtTeacherCurriculumoDao.findHCurriculumInfo(nType,pageIndex,pageSize);
				return list;
			}else{
				list=oHljtTeacherCurriculumoDao.findCurriculumInfo();
			}
		}else{
			if(nType==1){
				list=oHljtTeacherCurriculumoDao.findHCurriculumInfoByName(nType,name,pageIndex,pageSize);
				return list;
			}else{
				list=oHljtTeacherCurriculumoDao.findCurriculumInfoByName(name);
			}
		}
		HashMap<Integer,TeacherCurriculumInfo> oHashMap=new  HashMap<Integer,TeacherCurriculumInfo>();
		for (TeacherCurriculumInfo teacherCurriculumInfo : list) {
			boolean flag=oHashMap.containsKey(teacherCurriculumInfo.getTeacherId());
			if(flag){
			TeacherCurriculumInfo  oTeacherCurriculumInfo =	oHashMap.get(teacherCurriculumInfo.getTeacherId());
			HljtClass oHljtClass=new HljtClass();
			oHljtClass.setClassid(teacherCurriculumInfo.getClassId());
			oHljtClass.setGrade(teacherCurriculumInfo.getGrade());
			oHljtClass.setClasses(teacherCurriculumInfo.getClasses());
			oTeacherCurriculumInfo.getClist().add(oHljtClass);
			oHashMap.put(teacherCurriculumInfo.getTeacherId(), oTeacherCurriculumInfo);
			}else{
				HljtClass oHljtClass=new HljtClass();
				oHljtClass.setClassid(teacherCurriculumInfo.getClassId());
				oHljtClass.setGrade(teacherCurriculumInfo.getGrade());
				oHljtClass.setClasses(teacherCurriculumInfo.getClasses());
				teacherCurriculumInfo.getClist().add(oHljtClass);
				oHashMap.put(teacherCurriculumInfo.getTeacherId(), teacherCurriculumInfo);
			}
		}
		List<TeacherCurriculumInfo> repList=new ArrayList<TeacherCurriculumInfo> ();

		Set<Integer> oSet=oHashMap.keySet();
		for (Integer integer : oSet) {
			repList.add(oHashMap.get(integer));
		}
		return repList;
	}
	
	  
	@Override
	public String insertCurriculumInfo(int nType,String classIds,int teacherId ,boolean flag) {
		int rCount=0;
		int nCount=oHljtTeacherCurriculumoDao.selectCountByTeacherId(teacherId);
		RepMessage oRepMessage=new RepMessage();
		if(nCount!=0 & flag!=true){
			oRepMessage.setFlag(false);
			oRepMessage.setErrInfo("该老师已存在，请在调取修改接口");
			return MapperToJson.toJson(oRepMessage);
		}
		String subject=oHljtTeacherCurriculumoDao.selectSubjectByTeacherId(teacherId);
		if(nType==0){
			List<String> classIdList=new ArrayList<String>();
			if(classIds.length()<3){
				classIdList.add(classIds);
			}else{
				classIdList = java.util.Arrays.asList(classIds.split(","));
			}
			List<Integer> iList=new ArrayList<Integer>();
				for (String string : classIdList) {
					iList.add(Integer.parseInt(string));
				}
			List<TeacherCurriculumInfo> tList=new ArrayList<TeacherCurriculumInfo>();
			for (Integer classId : iList) {
				TeacherCurriculumInfo oTeacherCurriculumInfo=new TeacherCurriculumInfo();
				oTeacherCurriculumInfo.setClassId(classId);
				oTeacherCurriculumInfo.setTeacherId(teacherId);
				oTeacherCurriculumInfo.sethType(nType);
				oTeacherCurriculumInfo.setSubject(subject);
				tList.add(oTeacherCurriculumInfo);
			}
			
			if(tList.size()>0){
				rCount=	oHljtTeacherCurriculumoDao.insertTeacherCurriculumInfoList(tList);
			}
		}else{
			int cCount=	oHljtTeacherCurriculumoDao.selectCountByClassIdAndType(Integer.parseInt(classIds));
			if(cCount!=0){
				oRepMessage.setFlag(false);
				oRepMessage.setErrInfo("该班已有班主任");
				return MapperToJson.toJson(oRepMessage);
			}
			TeacherCurriculumInfo oTeacherCurriculumInfo=new TeacherCurriculumInfo();
			oTeacherCurriculumInfo.setClassId(Integer.parseInt(classIds));
			oTeacherCurriculumInfo.setTeacherId(teacherId);
			oTeacherCurriculumInfo.sethType(nType);
			oTeacherCurriculumInfo.setSubject(subject);
			rCount=	oHljtTeacherCurriculumoDao.insertTeacherCurriculumInfo(oTeacherCurriculumInfo);
		}
		oRepMessage.setFlag(true);
		oRepMessage.setObjData(rCount);
		WhetherNotToUpdate.setPattern(true);
		return MapperToJson.toJson(oRepMessage);
	}
	@Override
	public String updateCurriculumInfo(int nType,String classIds,String oldClassIds,int teacherId,int oldTeacherId  ) {
		RepMessage oRepMessage=new RepMessage();
		
		System.out.println("nType"+nType+"classIds"+classIds+"oldClassIds"+oldClassIds+"teacherId"+teacherId+"oldTeacherId"+oldTeacherId);
		
		if(nType==0){
			List<String> classIdList=new ArrayList<String>();
			if(classIds.length()<3){
				classIdList.add(classIds);
			}else{
				classIdList = java.util.Arrays.asList(classIds.split(","));
			}
			List<Integer> cList= new  ArrayList<Integer>();
			cList=oHljtTeacherCurriculumoDao.selectClassesByTeacherId(teacherId);
			String dIds="";
			String iIds="";
			for (Integer integer : cList) {
				String strValue=String.valueOf(integer);
				if(!classIdList.contains(strValue)){
					dIds+=strValue+",";
				}
			}
			if(classIdList.size()>1){
				for (String string : classIdList) {
					int intValue=Integer.parseInt(string);
					if(!cList.contains(intValue)){
						iIds+=intValue+",";
					}
				}
			}
			
			if(iIds.length()>0){
				iIds=iIds.substring(0, iIds.length()-1);
				this.insertCurriculumInfo(nType, iIds, teacherId,true);
			}
			if(dIds.length()>0){
				dIds=dIds.substring(0, dIds.length()-1);
				String message=this.deleteCurriculumInfo(nType, dIds, teacherId);
				return message;
			}
			oRepMessage.setFlag(true);
			
		}else{
			//将TeacherCurriculumInfo Type为1的改成0
			if(!classIds.equals(oldClassIds)){
				int cCount=	oHljtTeacherCurriculumoDao.selectCountByClassIdAndType(Integer.parseInt(classIds));
				if(cCount!=0){
					oRepMessage.setFlag(false);
					oRepMessage.setErrInfo("该班已有班主任");
					return MapperToJson.toJson(oRepMessage);
				}
			}
			if(teacherId!=oldTeacherId){
				int tCount=oHljtTeacherCurriculumoDao.selectCountByTeacherIdAndType(teacherId);
				if(tCount!=0){
					oRepMessage.setFlag(false);
					oRepMessage.setErrInfo("该老师已是其他班的班主任");
					return MapperToJson.toJson(oRepMessage);
				}
			}
			String subject=oHljtTeacherCurriculumoDao.selectSubjectByTeacherId(teacherId);
			int count=oHljtTeacherCurriculumoDao.selectCurriculumInfoByTeacherIdAndClassId(Integer.parseInt(classIds), teacherId);
			if(count==0){
				oHljtTeacherCurriculumoDao.updateTeacherTypeToZero(Integer.parseInt(oldClassIds), oldTeacherId);
				this.insertCurriculumInfo(1, classIds, teacherId,true);
			}else{
				oHljtTeacherCurriculumoDao.updateTeacherTypeToOne(Integer.parseInt(classIds), teacherId,subject);
				oHljtTeacherCurriculumoDao.updateTeacherTypeToZero(Integer.parseInt(oldClassIds),oldTeacherId);
			}
			oRepMessage.setFlag(true);
		}
		WhetherNotToUpdate.setPattern(true);
		return MapperToJson.toJson(oRepMessage);
	}

	@Override
	public String deleteCurriculumInfo(int nType,String classIds,int teacherId) {
		RepMessage oRepMessage=new RepMessage();
		if(nType==0){
			List<String> classIdList ;
			if(classIds.length()<3){
			 classIdList=new ArrayList<String>();
				classIdList.add(classIds);
			}else{
				classIdList =new ArrayList<String>(java.util.Arrays.asList(classIds.split(",")));
			}
			
			String hClassId=String.valueOf(oHljtTeacherCurriculumoDao.selectCurriculumInfoByTeacherIdAndType(1,teacherId));
			if(classIdList.contains(hClassId)){
				classIdList.remove(hClassId);
				oRepMessage.setErrInfo("班主任管理的班级不能在任课老师界面删除");
			}
			
	
		List<TeacherCurriculumInfo> talist=oHljtTeacherCurriculumoDao.selectTeacherCurriculumInfoByTeacherId(teacherId);
		
		List<Integer> IdI=new ArrayList<Integer>();
		for (TeacherCurriculumInfo teacherCurriculumInfo : talist) {
			if(classIdList.contains(String.valueOf(teacherCurriculumInfo.getClassId()))){
				IdI.add(teacherCurriculumInfo.getId());
			}
		}
			if(IdI.size()>0){
				oHljtTeacherCurriculumoDao.deleteCurrculumInfo(IdI);
				oRepMessage.setFlag(true);
				WhetherNotToUpdate.setPattern(true);
			}else{
				oRepMessage.setFlag(false);
			}
		}else{
			TeacherCurriculumInfo oTeacherCurriculumInfo=new TeacherCurriculumInfo();
			oTeacherCurriculumInfo.setClassId(Integer.parseInt(classIds));
			oTeacherCurriculumInfo.setTeacherId(teacherId);
			oTeacherCurriculumInfo.sethType(nType);
			oHljtTeacherCurriculumoDao.deleteHoCurrculumInfo(oTeacherCurriculumInfo);
			oRepMessage.setFlag(true);
			WhetherNotToUpdate.setPattern(true);
		}
		return MapperToJson.toJson(oRepMessage);
	}

}
